package nxu.it.controller;

import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.annotation.FormParam;
import io.jooby.annotation.GET;
import io.jooby.annotation.POST;
import io.jooby.annotation.Path;
import jakarta.inject.Singleton;
import nxu.it.model.Todo;
import security.NeedLogin;

import java.awt.datatransfer.MimeTypeParseException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/")
@Singleton
public class TodoController {
    List<Todo> todoList=new ArrayList<>();
    {
        todoList.add(new Todo("吃饭",true));
        todoList.add(new Todo("睡觉",true));
    }
    @GET(value = "/todo/list",produces = MediaType.HTML)
    @NeedLogin
    public ModelAndView list(Context ctx){
        Map<String, Object> model = new HashMap<>();
        model.put("todoList", todoList);
        return new ModelAndView("todo/list.ftl", model);
    }
    @POST(value = "/todo/save",produces = MediaType.HTML)
    @NeedLogin
    public ModelAndView save(String content){
        Todo todo=new Todo(content);
        todoList.add(todo);
        ModelAndView mv=new ModelAndView("todo/success.ftl",Map.of());
        return mv;
    }
    @POST(value = "/todo/delete",produces = MediaType.HTML)
    @NeedLogin
    public ModelAndView delete(String content){
        Todo todo=new Todo(content);
        todoList.remove(todo);
        ModelAndView mv=new ModelAndView("todo/delete.ftl",Map.of());
        return mv;
    }
    @POST(value = "/todo/updata",produces = MediaType.HTML)
    @NeedLogin
    public ModelAndView update(String content){
        Todo todo=new Todo(content);
        todoList.add(todo);
        ModelAndView mv=new ModelAndView("todo/update.ftl",Map.of());
        return mv;
    }
}
